/**
 * Created by IrisHuang on 2016/4/27.
 */
var tableCreate={

    setButton:function(type,data){
        if(type=="drop") {
            var ulwrap = $("<ul></ul>").addClass("dropdown-menu").attr("role", "menu");
            var length = data.length;
            for (var i = 0; i < length; i++) {
                var liInner = $("<li></li>");
                var link = $("<a></a>").attr("href", "#").text(data[i]);
                liInner.append(link);
                ulwrap.append(liInner);
            }
            var groupButton=$("<div></div>").addClass("btn-group");
            var tdBottonDrop=$("<button></button>").attr({type:"button"}).addClass("btn btn-xs btn-primary dropdown-toggle").attr("data-toggle","dropdown");
            tdBottonDrop.append("More <span class='caret'></span>");
            groupButton.append(tdBottonDrop,ulwrap).addClass("margin-before");
            return groupButton;
        }else if(type=="info") {
            var tdButtonInfo = $("<button></button>").addClass("btn btn-xs btn-info margin-before").text(data[0]);
            return tdButtonInfo; 
        }else if(type=="success") {
            var tdButtonRole = $("<button></button>").addClass("btn btn-xs btn-success margin-before").text(data[0]).attr("data-toggle","modal").attr("data-target","#rolePopUp");
            return tdButtonRole;
        }else if(type=="danger"){
            var tdButtonDanger = $("<button></button>").addClass("btn btn-xs btn-danger margin-before").text(data[0]);
            return tdButtonDanger;
        }

    },
    tableHead:function(data){
        var trDom=$("<tr></tr>");
        var inputTd=$("<td><input type='checkbox'></td>");
        trDom.append(inputTd);
        var col=data.length;
        for(var i=0;i<col;i++){
            var tdDom=$("<td></td>");
            var tdText=data[i];
            tdDom.text(tdText);
            trDom.append(tdDom);
        }
        $("thead").append(trDom);
    },
    tableBody:function(row,data,buttonDom){
        data.unshift(1);
        for(var j=0;j<row;j++){
            var trDom = $("<tr></tr>");
            var inputTd=$("<td><input type='checkbox'></td>");
            trDom.append(inputTd);
            var col = data.length;
            for (var i = 0; i < col; i++){
                var tdDom = $("<td></td>");
                var tdText = data[i];
                tdDom.text(tdText);
                trDom.append(tdDom);
            }
            var buttonNum=buttonDom.length;
            var lastTd=$("<td></td>");
            for(var z=0;z<buttonNum;z++){
                var buttonAttr=buttonDom[z].split(":");
                lastTd.append(this.setButton(buttonAttr[0],buttonAttr[1].split(",")));
            }
            trDom.append(lastTd);
            $("tbody").append(trDom);
            data[0]++;
        }
    },
    tableFoot:function(col,data){

    },
    pagenation:function(){

    }
}
var headData=["ID","Name","Birthday","Email","Phone Number","Action"];
var bodyData=["huangiris","920327","710383513@qq.com","15002897962"];

$(document).ready(function(){
    tableCreate.tableHead(headData);
    tableCreate.tableBody(10,bodyData,["info:Info","success:Role","drop:Project,Task,Timesheet"]);
}
);